define(function(require, exports, module) {
	J = jQuery;
	require('plugins');
	require('layer');
	require('jqueryform');
	require('bootstrap');
	require('bootstrapValidator');
	layer.config({
	    path: '/assets/vendors/layer/' //layer.js所在的目录，可以是绝对目录，也可以是相对目录
	});
	var timenow = new Date().getTime();
	//===========================================登录开始=================================================
	//用户登录名
	$("#login_username").on("blur", function(){
		var username=$("#login_username").val();
		if(username.length>0){
			if(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
    /*验证码重新加载*/
    $("#reloadCaptcha").click(function(){      
    	new_src = '/captcha/default?'+Math.random();
    	$(this).find("img").attr("src",new_src);
    });
    
    /*用户表单输入时删除错误提示*/
    $("body").delegate("form input","keydown",function(){
        $(this).parents(".form-group").removeClass("has-error");
        $(this).next(".help-block").remove();
    });
    
	//用户密码
	$("#login_password").on("blur", function(){
		var password=$("#login_password").val();
		if(password.length>=6){
			if(/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
	$("#login-btn").on("click", function(){
		var username=$("#username").val();
		var password=$("#password").val();
		var captcha=$("#captcha").val();
		var redirectUrl=$("#redirectUrl").val();
		var rememberMe=$("#login_rememberMe").val();
		
		if(username.length>0){
			if(!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#username").closest('div').removeClass('has-success').addClass('has-error');
				$("#username").focus();
				return false; 
			}
		}else{
			$("#login_username").closest('div').removeClass('has-success').addClass('has-error');
			$("#login_username").focus();
			return false; 
		}
		
		if(password.length>=6){
			if(!/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$("#password").closest('div').removeClass('has-success').addClass('has-error');
				$("#password").focus();
				return false; 
			}
		}else{
			$("#password").closest('div').removeClass('has-success').addClass('has-error');
			$("#password").focus();
			return false;
		}
		//layer.msg("11111", {icon: 5});
		$.ajax({
    		url: '/member/login.json', 
    		data: {'username': username,'password': password,'redirectUrl':redirectUrl,'captcha': captcha,'rememberMe':$("#rememberMe").is(':checked')},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(data){	
    			if(data.code==0){
    				window.location.href = data.url;
    				return false;
    			}else{
    				layer.msg(data.message, {icon: 2});
					return false;
				}
          	}
    	});
		return false;
	});
	$("#login-form").keydown(function(e){
		 var e = e || event,
		 keycode = e.which || e.keyCode;
		 if (keycode==13) {
		  $(".log_btn").trigger("click");
		 }
	});
	//===========================================登陆结束=================================================
	//===========================================注册开始=================================================
	$('#verify_code').on("click",function(event){
		//alert("ffff");
		var count = 60;
        var countdown = setInterval(CountDown, 1000);
        function CountDown() {
            $("#verify_code").attr("disabled", true);
            $("#verify_code").val("您在" + count + " 秒后可以再次获取!");
            if (count == 0) {
                $("#verify_code").val("重新获取验证码").removeAttr("disabled");
                clearInterval(countdown);
            }
            count--;
        }
    })
	$("#verify_code").on("click",function(event){
		var username = $("#username").val();
		if(username.length>0){
			if(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#username").closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$("#username").closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$("#username").closest('div').removeClass('has-success').addClass('has-error');
		}
		$.ajax({
    		url: "/member/mailcaptcha.json?"+timenow,
    		data: {"username":username},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(ret){
				if (ret.code >= 0) {
	        		return false;
				} else {
					layer.msg(ret.message, {icon: 5});
					return false;
				}
          	}
    	});
		return false;
	});
	
	//用户登录名
	$("#username").on("blur", function(){
		var username=$("#username").val();
		if(username.length>0){
			if(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
	//用户昵称
	$("#nickname").on("blur", function(){
		var nickname=$("#nickname").val();
		if(nickname.length>0){
			if(/^[\u4e00-\u9fa5a-zA-Z0-9_]{2,10}$/.test(nickname)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
	//用户密码
	$("#password").on("blur", function(){
		var password=$("#password").val();
		if(password.length>=6){
			if(/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
	//确认密码
	$("#password2").on("blur", function(){
		var password2=$("#password2").val();
		var password=$("#password").val();
		if(password2.length>0){
			if(password2!=password){
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}else{
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
	//用户注册提交
	$("#register-btn").on("click", function(){
		var username=$("#username").val();
		var nickname=$("#nickname").val();
		var password=$("#password").val();
		var password2=$("#password2").val();
		var captcha=$("#captcha").val();
		if(username.length>0){
			if(!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#username").closest('div').removeClass('has-success').addClass('has-error');
				$("#username").focus();
				return false; 
			}else{
				$("#username").closest('div').removeClass('has-error').addClass('has-success');
			}
		}else{
			$("#username").closest('div').removeClass('has-success').addClass('has-error');
			$("#username").focus();
			return false; 
		}
		
		if(nickname.length>=2){
			if(!/^[\u4e00-\u9fa5a-zA-Z0-9_]{2,10}$/.test(nickname)){
				$("#nickname").closest('div').removeClass('has-success').addClass('has-error');
				$("#nickname").focus();
				return false; 
			}else{
				$("#nickname").closest('div').removeClass('has-error').addClass('has-success');
			}
		}else{
			$("#nickname").closest('div').removeClass('has-success').addClass('has-error');
			$("#nickname").focus();
			return false;
		}
		
		if(password.length>=6){
			if(!/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$("#password").closest('div').removeClass('has-success').addClass('has-error');
				$("#password").focus();
				return false; 
			}else{
				$("#password").closest('div').removeClass('has-error').addClass('has-success');
			}
		}else{
			$("#password").closest('div').removeClass('has-success').addClass('has-error');
			$("#password").focus();
			return false;
		}
		
		if(password!=password2){
			$("#password2").closest('div').removeClass('has-success').addClass('has-error');
			$("#password2").focus();
			return false;
		}else{
			$("#password2").closest('div').removeClass('has-error').addClass('has-success');
		}
		if(captcha.length>0){
			if(!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#captcha").closest('div').removeClass('has-success').addClass('has-error');
				$("#captcha").focus();
				return false; 
			}else{
				$("#username").closest('div').removeClass('has-error').addClass('has-success');
			}
		}else{
			$("#captcha").closest('div').removeClass('has-success').addClass('has-error');
			$("#captcha").focus();
			return false; 
		}
		
		jQuery.ajax({
			type:"POST",
			url: "/member/addUser.json",
			data: {"username":username,"nickname":nickname,"password":password,"password2":password2,"captcha":captcha},
			dataType:"json",
			cache : false,
	        async: false, //ajax方法外可以调用变量
	        success : function(data) {
	        	if (data.code==0) {
	        		window.location.href = "/member/index";	
				}else{
					layer.msg(data.message, {icon: 2});
					return false;
				}
	        }
	    });
		return false;
	});
	
	$("#register-btn").keydown(function(e){
		 var e = e || event,
		 keycode = e.which || e.keyCode;
		 if (keycode==13) {
		  $("#register-btn").trigger("click");
		 }
	});
	
	//===========================================注册结束=================================================
	
    $(".js-trun").click(function() {
    	var $loginbox = $(this).closest(".login-box");
    	if($loginbox.hasClass("register")){         
          $loginbox.siblings().addClass("login-delay");
          $(".login-box").removeClass("active");
           return;
    	}
    	$(".login-box").addClass('active').removeClass("login-delay");
    });
    
	$("#password-reset-form").ajaxForm({
		dataType : 'json',
		success : function(data) {
			if(data.code==0){
				$('#password-reset input[type=submit]', this).attr('disabled', 'disabled');
				$("#reset_password").css("display","none");
				$("#reset_result").css("display","inline");
				//window.location.href = "/member/login";	
				setInterval(window.location.href = "/member/login",30000);
				return false;
			}else{
				layer.msg(data.message, {icon: 2});
				return false;
			}
		}
	});
	
	$("#reset-btn").on("click", function(){
		var uid=$("#uid").val();
		var password=$("#password").val();
		var password1=$("#password1").val();
		var code=$("#code").val();

		if(password.length>=6){
			if(!/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$("#password").closest('div').removeClass('has-success').addClass('has-error');
				$("#password").focus();
				return false; 
			}else{
				$("#password").closest('div').removeClass('has-error').addClass('has-success');
			}
		}else{
			$("#password").closest('div').removeClass('has-success').addClass('has-error');
			$("#password").focus();
			return false;
		}
		
		if(password!=password1){
			$("#password1").closest('div').removeClass('has-success').addClass('has-error');
			$("#password1").focus();
			return false;
		}else{
			$("#password1").closest('div').removeClass('has-error').addClass('has-success');
		}
		

		$.ajax({
    		url: '/member/reset.json', 
    		data: {'uid': uid,'password': password,'password1': password1,'code': code},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(data){	
    			if(data.code==0){
    				//弹出提示5秒后刷新页面
    				layer.msg(data.message,{icon:1,time:5000},function(){
    	                window.location.href = "/member/login";
    	            });
    				return false;
    			}else{
    				layer.msg(data.message, {icon: 2});
				}
          	}
    	});
	});
});